package ckw.communication;

import javax.inject.Inject;
import javax.management.MBeanServer;

import org.jboss.remoting.InvocationRequest;
import org.jboss.remoting.ServerInvoker;
import org.jboss.remoting.callback.InvokerCallbackHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;

import core.communication.message.Message;

@Controller
public class InvocationHandlerImpl implements InvocationHandler {

	private static final Logger L = LoggerFactory.getLogger(InvocationHandlerImpl.class);

	private CommunicationService communicationService;

	@Inject
	public InvocationHandlerImpl(CommunicationService communicationService) {
		L.debug("Tworzę InvocationHandler");

		Assert.notNull(communicationService);

		this.communicationService = communicationService;

		L.debug("Utworzono InvocationHandler");
	}

	@Override
	public Object invoke(InvocationRequest arg0) throws Throwable {
		Message message = (Message) arg0.getParameter();
		L.info("Otrzymano wiadomość " + message.toString());
		communicationService.addReceivedMessage(message);
		return true;
	}

	@Override
	public void addListener(InvokerCallbackHandler arg0) {
	}

	@Override
	public void removeListener(InvokerCallbackHandler arg0) {
	}

	@Override
	public void setInvoker(ServerInvoker arg0) {
	}

	@Override
	public void setMBeanServer(MBeanServer arg0) {
	}
}
